//
//  AskingCollectionViewCell.m
//  TodatPlay
//
//  Created by dllo on 16/9/6.
//  Copyright © 2016年 DM.C. All rights reserved.
//

#import "AskingCollectionViewCell.h"
#import "TAMacro.h"
#import "AskingSmallCollectionViewCell.h"
#import "SDAutoLayout.h"
#import "AskingTableViewCell.h"
#import "CDMNetTool.h"
// ASking model
#import "AskingRootClass.h"
#import "AskingData.h"
#import "AskingDatalist.h"
#import "AskingModelOfData.h"
#import "AskingModelList.h"

#define kTableViewAskingReuse @"askingReuse"
@interface AskingCollectionViewCell ()<UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *askingTableView;
@property (nonatomic, strong) AskingRootClass *askingRootModel;
@end

@implementation AskingCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self creatTableView];

}
- (void)creatTableView {
    self.askingTableView.delegate = self;
    self.askingTableView.dataSource = self;
    [self.askingTableView registerClass:[AskingTableViewCell class] forCellReuseIdentifier:kTableViewAskingReuse];
    self.askingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.askingTableView.backgroundColor = [UIColor colorWithWhite:0.942 alpha:1.000];
    
}
- (void)setParmID:(NSString *)parmID {
    _parmID = parmID;
    [self.askingTableView reloadData];
    [self handleASkingData];
}

- (void)handleASkingData {
    NSString *HTTPStr = [NSString stringWithFormat:@"%@/%@", @"http://api.jtwsm.cn/api/01300101", _parmID];
    NSString *paramStr = @"platform=4&channel_from=m360_market&source=10345&pi=0&ps=10&version=203004";
    NSString *finalStr = [HTTPStr stringByAppendingFormat:@"?%@", paramStr];
//    NSLog(@"%@", finalStr);
    [CDMNetTool GET:finalStr andBody:nil andHeader:nil andResponse:CDMJSON andSuccessBlock:^(id result) {
//        NSLog(@"请求成功%@", result);
        self.askingRootModel = [AskingRootClass modelWithDic:result];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败%@", error);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _askingRootModel.data.datalist.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AskingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableViewAskingReuse];
    cell.userNameLabel.text = self.askingRootModel.data.datalist[indexPath.row].data.username;
   
    /***************** 时间戳转换 ******************/
    
    
    NSDateFormatter *inputFormatter= [[NSDateFormatter alloc] init];
    
    [inputFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] ];
    
    NSDate*inputDate = [inputFormatter dateFromString:[NSString stringWithFormat:@"%ld", self.askingRootModel.data.datalist[indexPath.row].data.createDate]];
    
    
    NSDateFormatter *outputFormatter= [[NSDateFormatter alloc] init];
    
    [outputFormatter setLocale:[NSLocale currentLocale]];
    
    [outputFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];
    
    NSString *str= [outputFormatter stringFromDate:inputDate];
    NSLog(@"%@", str);
    /***************** 转换结束 ******************/
    cell.timeLabel.text = str;
    cell.contentsLabel.text = self.askingRootModel.data.datalist[indexPath.row].data.msg;
    cell.contentView.backgroundColor = [UIColor colorWithWhite:0.949 alpha:1.000];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [self cellHeightForIndexPath:indexPath cellContentViewWidth:SCREEN_WIDTH - 10 tableView:self.askingTableView];
}
+ (NSString *)getDateStringWithDate:(NSDate *)date
                         DateFormat:(NSString *)formatString
{
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:formatString];
    NSString *dateString = [dateFormat stringFromDate:date];
    
    
    return dateString;
}

#pragma mark - 反回头视图和头视图的高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headVeiw = [UIView new];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [headVeiw addSubview:button];
    [button setTitle:@"我要提问" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button addTarget:self action:@selector(handleAsking:) forControlEvents:UIControlEventTouchUpInside];
    
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    button.layer.cornerRadius = 5;
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor blackColor].CGColor;
    button.layer.masksToBounds = YES;
    
    headVeiw.sd_layout
    .leftEqualToView(self.askingTableView)
    .topEqualToView(self.askingTableView)
    .rightEqualToView(self.askingTableView)
    .heightIs(60);
    button.sd_layout
    .centerXEqualToView(headVeiw)
    .centerYEqualToView(headVeiw)
    .heightIs(30)
    .widthIs(SCREEN_WIDTH / 2.0);
    
    return headVeiw;
}
#pragma mark - 实现我要提问按钮的方法
- (void)handleAsking:(UIButton *)button {
    NSLog(@"点了我要提问");
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 60;
}
#pragma mark - 不让区头视图滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView == self.askingTableView)
    {
        CGFloat sectionHeaderHeight = 60;
        if (scrollView.contentOffset.y <= sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        }
    }
}

@end
